// 671. 二叉树中第二小的节点
// AC：https://leetcode.cn/problems/second-minimum-node-in-a-binary-tree/submissions/621117694/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int m;
    int mt=-1;
    void dfs(TreeNode* root){
        if(root==nullptr)
            return;
        if(mt!=-1&&root->val>=mt)
            return;
        if(root->val>m)
            mt=root->val;
        dfs(root->left);
        dfs(root->right);
    }
    int findSecondMinimumValue(TreeNode* root) {
        m=root->val;
        dfs(root);
        return mt;
    }
};